        <h3>Message Client</h3>
<?php
    include('../inc/config.inc.php');
    require('../inc/pm-a.php');
    
    // Set the userid to 2 for testing purposes... you should have your own usersystem, so this should contain the userid
    $userid = $_SESSION['admin_id'];
	//echo $userid;
    
    $pm = new cpm($userid);
    
    // check if a new message had been send
    if(isset($_POST['newmessage'])){
        if($pm->sendmessage($_POST['to'],$_POST['subject'],$_POST['message'])) {
            echo "Message successfully sent!<br />";
        }else{
            echo "Error, couldn't send PM. Maybe wrong user.<br />";
        }
    }
    if(isset($_POST['delete'])){
        if($pm->deleted($_POST['did'])){
            echo "Message successfully deleted!<br />";
        }else{
            echo "Error, couldn't delete PM!<br />";
        }
    }

// In this switch we check what page has to be loaded, this way we just load the messages we want using numbers from 0 to 3 (0 is standart, so we don't need to type this)
    if(isset($_GET['p'])){
        switch($_GET['p']){
            case 'new': $pm->getmessages(); break;
            case 'sent': $pm->getmessages(2); break;
            case 'read': $pm->getmessages(1); break;
            case 'deleted': $pm->getmessages(3); break;
            case 'view': $pm->getmessage($_GET['mid']); break;
            default: $pm->getmessages(); break;
        }
    }else{
        // get all new / unread messages
        $pm->getmessages();
    }
?>    
    <?php
    
    // if it's the standart startpage or the page new, then show all new messages
    if(!isset($_GET['p']) || $_GET['p'] == 'new') {
    ?>
    <table border="0" cellspacing="1" cellpadding="1">
    <tr>
		<th>From</th>
		<th>Title</th>
		<th>Date</th>
    </tr>
    <?php
    
    // If there are messages, show them
    if(count($pm->messages)) {
        // message loop
        for($i=0;$i<count($pm->messages);$i++) {
            ?>
            <tr>
                <td><?php echo $pm->messages[$i]['from']; ?></td>
                <td><a href='?c=message&p=view&mid=<?php echo $pm->messages[$i]['id']; ?>'><?php echo $pm->messages[$i]['title'] ?></a></td>
                <td><?php echo $pm->messages[$i]['created']; ?></td>
            </tr>
            <?php
        }
    } else {
        // else... tell the user that there are no new messages
        echo "<tr><td colspan='3'><b>No new messages found</b></td></tr>";
    }
    ?>
    </table>
    <?php
    // check if the user wants send messages
    } elseif($_GET['p'] == 'sent') {
    ?>
    <table>
	<thead>
		<tr>
			<th>To</th>
			<th>Title</th>
			<th>Status</th>
			<th>Date</th>
		</tr>
	</thead>
		<tbody>
		<?php
		if(count($pm->messages)) {
			for($i=0;$i<count($pm->messages);$i++) {
				?>
				<tr>
					<td><?php echo $pm->messages[$i]['to']; ?></td>
					<td><a href='?c=message&p=view&mid=<?php echo $pm->messages[$i]['id']; ?>'><?php echo $pm->messages[$i]['title'] ?></a></td>
					<td>
				<?php  
				
				// If a message is deleted and not viewed
				if($pm->messages[$i]['to_deleted'] && !$pm->messages[$i]['to_viewed']) {
					echo "Deleted without reading";
					// if a message got deleted AND viewed
				} elseif($pm->messages[$i]['to_deleted'] && $pm->messages[$i]['to_viewed']) {
					echo "Deleted after reading";
					// if a message got not deleted but viewed
				} elseif(!$pm->messages[$i]['to_deleted'] && $pm->messages[$i]['to_viewed']) {
					echo "Read";
				} else {
					// not viewed and not deleted
					echo "Not read yet";
				}
				?>
					</td>
					<td><?php echo $pm->messages[$i]['created']; ?></td>
					
				</tr>
				<?php
			}
		}else{    
			echo "<tr><td colspan='4'><b>No sent messages found</b></td></tr>";
		}
		?>
		</tbody>
    </table>
    <?php
    // check if the user wants the read messages
    }elseif($_GET['p'] == 'read'){
        ?>
        <table border="0" cellspacing="1" cellpadding="1">
        <tr>  
			<th>From</th>  
			<th>Title</th> 
			<th>Date</th>
        </tr>
        <?php
        // if there are messages, show them
        if(count($pm->messages)){
            // message loop  
            for($i=0;$i<count($pm->messages);$i++){ 
                ?>  
                <tr> 
                <td><?php echo $pm->messages[$i]['from']; ?></td> 
                <td><?php echo $pm->messages[$i]['to_vdate']; ?></td>
                </tr>  
                <?php  
            }   
        } else {  
        // else... tell the user that there are no new messages 
        echo "<tr><td colspan='4'><b>No read messages found</b></td></tr>";
        } 
        ?> 
        </table>
        <?php
    // check if the user wants the deleted messages
    } elseif($_GET['p'] == 'deleted') {
    ?>
    <table border="0" cellspacing="1" cellpadding="1">
        <tr>
            <th>From</th>
            <th>Title</th>  
            <th>Date</th> 
        </tr>
        <?php
        // if there are messages, show them
        if(count($pm->messages)){
            // message loop
            for($i=0;$i<count($pm->messages);$i++) {
                ?> 
                <tr>
                    <td><?php echo $pm->messages[$i]['from']; ?></td>
                    <td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $pm->messages[$i]['id']; ?>'><?php echo $pm->messages[$i]['title'] ?></a></td>  
                    <td><?php echo $pm->messages[$i]['to_ddate']; ?></td>  
                </tr>
                <?php
                
            }
        }else{
        // else... tell the user that there are no new messages
        echo "<tr><td colspan='4'><div class='notice'><b>No deleted messages found</b></div></td></tr>";
        }
        ?>
    </table>
    <?php 
	} elseif($_GET['p'] == 'send') {
	?>
	    
    <form name="new" method="post" action="?c=message">
        <label>To:</label><br />
        <input type='text' name='to' class='text' value='<?php if(isset($_POST['reply'])) { echo $_POST['rfrom']; } ?>' /><br />
        <label>Subject:</label><br />
        <input type='text' name='subject' class='text' value='<?php if(isset($_POST['reply'])) { echo $_POST['rsubject']; } ?>' /><br />
        <label>Message:</label><br  />
        <textarea name='message'><?php if(isset($_POST['reply'])) { echo $_POST['rmessage']; } ?></textarea><br />
        <input type='submit' name='newmessage' value='Send' />
    </form>
    <?php
    // if the user wants a detail view and the message id is set...
    } elseif($_GET['p'] == 'view' && isset($_GET['mid'])) {
    // if the users id is the recipients id and the message hadn't been viewed yet
    if($userid == $pm->messages[0]['toid'] && !$pm->messages[0]['to_viewed']) {
    // set the messages flag to viewed
    $pm->viewed($pm->messages[0]['id']);
    }
    ?>
    <table border="0" cellspacing="1" cellpadding="1">
        <tr>
            <td><b>From:</b></td>
            <td><?php echo $pm->messages[0]['from']; ?></td>
            <td colspan="2"></td>
        </tr>
        <tr>
            <td><b>Date:</b></td>
            <td><?php echo $pm->messages[0]['created']; ?></td>
            <td colspan="2"></td>
        </tr>
        <tr>
            <td><b>Subject:</b></td>
            <td colspan="3"><?php echo $pm->messages[0]['title']; ?></td>
        </tr> 
            <tr>
            <td colspan="4" class="quiet"><?php echo $pm->render($pm->messages[0]['message']); ?></td>
        </tr>
    </table>
        <form name='reply' method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
        <input type='hidden' name='rfrom' value='<?php echo $pm->messages[0]['from']; ?>' />
        <input type='hidden' name='rsubject' value='Re: <?php echo $pm->messages[0]['title']; ?>' />
        <input type='hidden' name='rmessage' value='[quote]<?php echo $pm->messages[0]['message']; ?>[/quote]' />
        <input type='submit' name='reply' value='Reply' />
    </form>
    
    <form name='delete' method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
        <input type='hidden' name='did' value='<?php echo $pm->messages[0]['id']; ?>' />
        <input type='submit' name='delete' value='Delete' />
    </form>
    <?php
    
    }
    
    ?>
